package com.xie.o2o.controller.frontend;

import com.google.common.collect.Maps;
import com.xie.o2o.dto.ProductExecution;
import com.xie.o2o.entity.Product;
import com.xie.o2o.entity.ProductCategory;
import com.xie.o2o.entity.Shop;
import com.xie.o2o.service.ProductCategoryService;
import com.xie.o2o.service.ProductService;
import com.xie.o2o.service.ShopService;
import com.xie.o2o.util.HttpServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @create: xie
 */
@Controller
@RequestMapping("/frontend")
public class ShopDetailController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 点击店铺获取详情页
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listshopdetailpageinfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> listShopDetail(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        //获取前台传递的shopid
        long shopId = HttpServletUtil.getLong(request, "shopId");
        Shop shop = null;
        List<ProductCategory> productCategoryList = null;
        if (shopId != -1) {
            shop = shopService.getByShopId(shopId);
            productCategoryList = productCategoryService.queryProductCategoryList(shopId);
            map.put("shop", shop);
            map.put("productCategoryList", productCategoryList);
            map.put("success", true);
        } else {
            map.put("success", false);
            map.put("errMsg", "empty shopId");
        }
        return map;
    }

    /**
     * 根据条件分页列出店铺下面所有商品
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listproductsbyshop", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> listProductsByShop(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        //获取页码
        int pageIndex = HttpServletUtil.getInt(request, "pageIndex");
        //获取每页显示数量
        int pageSize = HttpServletUtil.getInt(request, "pageSize");
        long shopId = HttpServletUtil.getLong(request, "shopId");

        if (pageIndex > -1 && pageSize > -1 && shopId > -1) {
            //尝试获取商品类别id和模糊查询name
            long productCategoryId = HttpServletUtil.getLong(request, "productCategoryId");
            String productName = HttpServletUtil.getString(request, "productName");
            //组合查询条件
            Product product = new Product();
            Shop shop = new Shop();
            shop.setShopId(shopId);
            product.setShop(shop);
            if (productCategoryId != -1) {
                ProductCategory productCategory = new ProductCategory();
                productCategory.setProductCategoryId(productCategoryId);
                product.setProductCategory(productCategory);
            }
            if (productName != null) {
                product.setProductName(productName);
            }
            //只允许显示上架的商品
            product.setEnableStatus(1);
            //根据查询条件查询商品列表
            ProductExecution productList = productService.getProductList(product, pageIndex, pageSize);
            map.put("productList", productList.getProductList());
            map.put("count", productList.getProductCount());
            map.put("success", true);
        } else {
            map.put("success", false);
            map.put("errMsg", "empty pageSize or pageIndex or shopId");
        }
        return map;
    }
}
